package com.food.user.consumer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.food.user.entity.Order;
import com.food.user.entity.OrderLog;
import com.food.user.service.OrderLogService;
import com.food.user.service.OrderService;
import com.food.user.util.DateUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@RocketMQMessageListener(consumerGroup = "modelRider",topic = "rider_receive")
public class RiderReceiverConsumer implements RocketMQListener<String> {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderLogService orderLogService;

    @Override
    public void onMessage(String s) {
        Order o = orderService.getById(s);
        //配送人和电话
        o.setOrderSendUser("金牌骑手");
        o.setOrderSendPhone("8888888888");
        //配送方式
        o.setOrderDispatching("地面运输");
        //预计送达时间
        String orderOrderDate = o.getOrderReceiveDate();
        DateTime dateTime = new DateTime(orderOrderDate);
        long l = dateTime.getTime() + 60 * 60 *1000;
        DateTime date = DateUtil.date(l);
        String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
        o.setOrderExpectReachDate(format);
        //修改状态为配送中
        o.setOrderState(3);
        o.setOrderStateName("派送中");
        orderService.updateById(o);


        //记录日志
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderLogOrderNum(s);
        orderLog.setOrderLogOrderStateId(3);
        orderLog.setOrderLogOrderState("派送中");
        orderLog.setOrderLogOperContent("骑手派送中");
        orderLog.setOrderLogOperName("系统自动记录日志");
        orderLog.setOrderLogOperUser("骑手");
        orderLog.setOrderLogOperDate(DateUtils.parseString(LocalDateTime.now(),"yyyy-MM-dd HH:mm:ss"));
        orderLogService.save(orderLog);
    }
}
